<template>
  <!-- Banner & Image 通用组件 -->
  <div class="image-content">
    <!-- <p class="desc">添加图片 (最多{{ len }}张，可拖动排序）</p> -->
    <p class="desc">添加图片 (最多1张）</p>

    <template>
      <el-button
        type="primary"
        icon="el-icon-plus"
        @click="addImage(null)"
        class="add-image"
      >
        更换图片
      </el-button>
      <p class="size">（建议尺寸：{{ size }}）</p>
    </template>
    <el-upload
      ref="upload"
      :http-request="httpRequest"
      :show-file-list="false"
      multiple
      action
      style="display: none"
    >
    </el-upload>
  </div>
</template>

<script>
import DlUpload from '@/components/common/DlUpload'
export default {
  name: 'Images',
  components: {
    DlUpload,
  },
  props: {
    comData: {
      // 这个模块的对象数据
      type: Object,
      default: () => {},
    },
  },
  data() {
    return {
      type: 'Images',
      list: {},
      //   图片
      fileTypes: ['JPEG', 'JPG', 'PNG'],
      image: [],
      picture: [],
      files: {},
    }
  },
  computed: {
    size() {
      return this.list.type == 'Images' ? '750*750' : '750*400'
    },
    len() {
      return this.list.type == 'Images' ? 1 : 10
    },
  },
  mounted() {
    this.list = this.comData
  },
  methods: {
    addImage() {
      this.$refs['upload'].$children[0].$refs.input.click()
    },
    // 上传照片
    async httpRequest(content) {
      try {
        const formData = new FormData()
        formData.append('file', content.file)
        const res = await this.$http.post(
          this.$API.baseApi.uploadImage,
          formData
        )
        if (res.data.code === 200 && res.data.success) {
          this.$set(this.list, 'images', res.data.result)
        }
      } catch (e) {
        this.$message.error(e)
      }
    },
  },
}
</script>

<style lang="scss" scoped>
.image-content {
  display: flex;
  flex-direction: column;
  flex-wrap: nowrap;
  height: 100%;
  overflow: hidden;
  .desc {
    text-align: center;
    font-size: 12px;
    color: #666;
    margin: 18px 0;
    padding-bottom: 10px;
    border-bottom: 1px dashed #ddd;
  }
  .size {
    text-align: center;
    font-size: 12px;
    color: #999;
    margin-bottom: 0;
  }
  .add-image {
    width: calc(100% - 20px);
    height: 34px;
    line-height: 34px;
    padding: 0;
    font-size: 12px;
    margin-left: 10px;
    margin-top: 10px;
  }
  .image-list {
    margin: 0;
    padding: 0 10px;
    overflow: auto;
    &::-webkit-scrollbar-thumb {
      background: #dbdbdb;
      border-radius: 10px;
    }
    &::-webkit-scrollbar-track {
      background: #f6f6f6;
      border-radius: 10px;
    }
    &::-webkit-scrollbar {
      width: 6px;
    }
    li {
      list-style: none;
      background: #f7f8f9;
      border-radius: 4px;
      padding: 6px 14px 10px;
      margin-bottom: 10px;
      display: flex;
      justify-content: space-between;
      &.disable div {
        pointer-events: none;
        user-select: none;
      }
      .l-info {
        font-size: 12px;
        padding-top: 8px;
        width: calc(100% - 70px);
        p {
          margin: 12px 0 0;
          white-space: nowrap;
          overflow: hidden;
          display: flex;
          .link {
            color: #1b8bff;
            cursor: pointer;
          }
          .text {
            white-space: nowrap;
            text-align: -webkit-auto;
            text-overflow: ellipsis;
            overflow: hidden;
          }
        }
      }
      .r-image {
        text-align: right;
        .el-icon-close {
          color: #999;
          font-size: 12px;
          font-weight: 600;
          margin-bottom: 6px;
          cursor: pointer;
          &:hover {
            color: red;
          }
        }
        .image-box {
          width: 70px;
          height: 70px;
          border-radius: 5px;
          overflow: hidden;
          position: relative;
          background: #fff;
          display: flex;
          justify-content: center;
          align-items: center;
          &::before {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background: rgba(0, 0, 0, 0.5);
            opacity: 0;
            transition: all 0.3s;
          }
          span {
            position: absolute;
            top: 50%;
            left: 50%;
            color: #fff;
            transform: translate(-50%, -50%);
            font-size: 20px;
            cursor: pointer;
            opacity: 0;
            transition: all 0.3s;
          }
          img {
            max-width: 100%;
          }
          &:hover {
            &::before,
            span {
              opacity: 1;
            }
          }
        }
      }
    }
  }
}
</style>